#version 330

#define float4x4 mat4
#define float2 vec2
#define float3 vec3
#define float4 vec4
#define float3x3 mat3

struct lightparam
{
	vec4 centerradius;
	vec4 color;
};

#define num_lights 4

layout(std140) uniform global
{
	mat4 cam_mtx;
	mat4 proj_mtx;
	mat4 viewproj_mtx;
	mat4 world_mtx;
	vec4 object_color;
	vec4 directional_light;
	vec2 stereo_dir;

	lightparam lights[num_lights];
};

float saturate(float a)
{
	return clamp(a,0,1);
}

float4 get_light_color(float3 pos,float3 normal)
{
	float4 ret=float4(0,0,0,0);
	for (int n=0; n<num_lights; ++n)
	{
		float3 center=lights[n].centerradius.xyz;
		float radius=lights[n].centerradius.w;
		float4 color=lights[n].color;
		float3 dir=center-pos;
		float dist=length(dir);
		normalize(dir);
		ret+= saturate(dot(dir,normal))*saturate(1-dist/radius)*color;
	}
	
	return ret;
}